//
//  YHSyntheticImageVC.m
//  LyhToolsTest
//
//  Created by LYH on 2018/11/8.
//  Copyright © 2018年 LYH.com. All rights reserved.
//

#import "YHSyntheticImageVC.h"

@interface YHSyntheticImageVC ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation YHSyntheticImageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initnavabar];
    UIButton *synthBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, SafeAreaTopHeight, 80, 40)];
    synthBtn.title = @"多张图片合成";
    synthBtn.titleFont = 13;
    synthBtn.titleColor = [UIColor blackColor];
    synthBtn.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [synthBtn addTarget:self action:@selector(synthClick)];
     UIButton *TitleBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-100, SafeAreaTopHeight, 80, 40)];
    TitleBtn.title = @"文字图片合成";
    TitleBtn.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [TitleBtn addTarget:self action:@selector(TitleClick)];
    TitleBtn.titleFont = 13;
    TitleBtn.titleColor = [UIColor blackColor];
    [self.view addSubview:synthBtn];
    [self.view addSubview:TitleBtn];
    
    self.imageView = [[UIImageView alloc]init];
    self.imageView.frame = CGRectMake(15, SafeAreaTopHeight+80, kScreenWidth-30, kScreenHeigth-SafeAreaTopHeight-80);
    [self.view addSubview:self.imageView];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
   
    
}
- (void)initnavabar
{
    WeakSelf;
    wself.ts_navgationBar = [TSNavigationBar navWithTitle:@"图片文字合成" backAction:^{
        [wself.navigationController popViewControllerAnimated:YES];
    }];
}
#pragma -mark ————————————————————Action————————————————————
//合成图片
- (void)synthClick
{
     self.imageView.image = [self SynthPicture:@"图层2" getImagename2:@"图层3" getImage3:@"图层4" SetimageSize:self.imageView.size];
}
//文字图片合成
- (void)TitleClick
{
    self.imageView.image = [self CSImage:@"图层2" Setimage2:@"图层4" AddTitle:@"【面包糕点】甜曲紫面包550g" detaile:@"券后价：¥9.90" setSize:self.imageView.size];
}

// 纯图片的合成
- (UIImage *)SynthPicture:(NSString *)imagename1 getImagename2:(NSString *)imagename2 getImage3 :(NSString *)imagename3 SetimageSize:(CGSize)imagesize
{
    CGFloat sysHight = imagesize.height;
    CGFloat sysWhight = imagesize.width;
    
    //开启图形上下文
    UIGraphicsBeginImageContext(CGSizeMake(sysWhight,sysHight));
    //需要合成的图片
     UIImage *image1 = [UIImage imageNamed:imagename1];
     UIImage *image2 = [UIImage imageNamed:imagename2];
     UIImage *image3 = [UIImage imageNamed:imagename3];
    //画1，2，3
     [image1 drawInRect:CGRectMake(0,0,sysWhight,sysHight *2/3)];
     [image2 drawInRect:CGRectMake(0,sysHight *2/3,sysWhight/2,sysHight/3)];
     [image3 drawInRect:CGRectMake(sysWhight/2,sysHight *2/3,sysWhight/2,sysHight/3)];
    //根据图形上下文拿到图片
    UIImage *synthimage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文
    UIGraphicsEndImageContext();
    return synthimage;
}
//图片文字合成 布局灵活，可控性强
/**
 *1先布局一个view，可以在xib上布局（xib的大小，要和设置的大小一样）
  2再把文字，图片加到view上
  3再把view绘成一张图
 */
- (UIImage *)CSImage:(NSString *)imagename1 Setimage2:(NSString *)imagename2 AddTitle:(NSString *)Title detaile:(NSString *)detailetext setSize:(CGSize)viewsize
{
    UIView *contview = [[UIView alloc]init];
    contview.backgroundColor = RGBColor(240, 240, 240);
    contview.size = viewsize;
    CGFloat contwidth = viewsize.width;
    CGFloat contHeight = viewsize.height;
    
    UIImageView *imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, contwidth, contHeight *3/4)];
    imageview1.image = [UIImage imageNamed:imagename1];
    
     UIImageView *imageview2 = [[UIImageView alloc]initWithFrame:CGRectMake(contwidth -contHeight/4,contHeight *3/4 , contHeight/4, contHeight/4)];
     imageview2.image = [UIImage imageNamed:imagename2];
    [contview addSubview:imageview1];
    [contview addSubview:imageview2];
    UILabel *titlelab = [[UILabel alloc]initWithFrame:CGRectMake(15, contHeight *3/4, contwidth-contHeight/4 - 30, 40)];
    titlelab.text = Title;
    titlelab.numberOfLines = 0;
    titlelab.textColor = RGBColor(24, 24, 24);
    titlelab.font = [UIFont systemFontOfSize:14];
    UILabel *desText = [[UILabel alloc]initWithFrame:CGRectMake(15, contHeight *3/4+50, contwidth-contHeight/4 - 30, 30)];
    desText.font = [UIFont systemFontOfSize:17];
    desText.textColor = RGBColor(140, 48, 39);
    desText.text = detailetext;
    [contview addSubview:titlelab];
    [contview addSubview:desText];
    
    
return  [self coverViewToimage:contview];
}
//view 绘制成图片
- (UIImage *)coverViewToimage:(UIView *)imageview
{
    CGSize imagesize = imageview.size;
    
    UIGraphicsBeginImageContextWithOptions(imagesize, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [imageview.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageview.layer.contents = nil;
    return image;
}


@end
